<template>
	<view class="search">
		<view class="search-box">
			<text class="search-left-icon"></text>
			<input class="search" type="text" v-model="searchVal" placeholder="请输入要搜索的内容" />
			<text class="search-right-icon" @click="getSearchNewsList">搜索</text>
		</view>

		<view class="nav-ul-list">
			<block v-for="item in searchNavList" :key="item.id">
				<view class="nav-li-content" @click="handoff(item.id)">
					<view class="nav-title">{{item.title}}</view>
					<text class="underline" :class="navIndex==item.id?'active':''"></text>
				</view>
			</block>
			<!-- <view class="nav-li-content">
				<view class="nav-title">News</view>
				<text class="underline"></text>
			</view>
			<view class="nav-li-content">
				<view class="nav-title">News</view>
				<text class="underline"></text>
			</view> -->
		</view>

		<view class="search-nav-list">
			<swiper class="scroll-view-height" @change="swipeIndex" :current="navIndex" :duration="300">

				<!-- 新闻 -->
				<swiper-item>
					<scroll-view scroll-y="true" class="scroll-height">
						<view class="section-list">
							<view v-if="searchList.length == 0" class="search-null">
								<view class="search-null-header">
									<view class="null-left"></view>
									<view class="null-right">猜你想搜</view>
								</view>
								<view class="null-searchlist">
									<block v-for="item in 1" :key="item">
										<view class="want-search-text">2023世界读书日</view>
									</block>
									<view class="want-search-text">宇宙探索编辑部</view>
									<view class="want-search-text">不良人第九集</view>
									<view class="want-search-text">我想就这样在人间行走</view>
									<view class="want-search-text">如何评价灌篮高手电影？</view>
								</view>
							</view>
							<view v-else class="ul-list">
								<block v-for="item in searchList" :key="item.id">
									<view class="li-list" v-if="item.media!=null" @click="goDetail(item.id)">
										<view class="left-img">
											<image v-if="/.*thepaper.*/gi.test(item.head_img)"
												:src="`https://images.weserv.nl/?url=${item.head_img}`"
												mode="aspectFill"></image>
											<image v-else :src="`${item.head_img}`" mode="aspectFill"></image>
										</view>
										<view class="content-writing">
											<view class="p-location">{{item.tag}}</view>
											<view class="p-title">{{item.title}}</view>
											<view class="p-come">
												<view class="p-come-left">
													<view class="p-person">
														<view class="person-icon">
															<image :src="item.user_avators" mode="aspectFit"></image>
														</view>
														<text class="person">{{item.media}}</text>
													</view>
													<view class="p-time">{{item.publish_time}}</view>
												</view>
												<view class="p-come-right">
													<!-- <text class="come-right-icon"></text> -->
												</view>
											</view>
										</view>
									</view>
								</block>
							</view>
						</view>
					</scroll-view>
				</swiper-item>

				<!-- 主题 -->
				<swiper-item>
					<scroll-view scroll-y="true" class="scroll-height">
						<block v-for="item in topicList" :key="'t'+item.id">
							<view class="ul-topic" @click="goTopic(item.title)">
								<view class="li-topic">
									<view class="topic-img">
										<image :src="item.url" mode="aspectFill"></image>
									</view>
									<view class="topic-desc">
										<view class="desc-title">{{item.title}}</view>
										<view class="desc-exp">{{item.num}}+ 文章</view>
									</view>
								</view>
							</view>
						</block>
					</scroll-view>
				</swiper-item>

				<!-- 媒体 -->
				<swiper-item>
					<scroll-view scroll-y="true" class="scroll-height">
						<view class="ul-author">
							<block v-for="(item,index) in mediaList" :key="'c'+ item.id">
								<view class="li-author">
									<view class="author-left">
										<view class="author-img">
											<image :src="item.logo" mode="aspectFit"></image>
										</view>
										<view class="author-name">{{item.name}}</view>
									</view>
									<view class="author-right" @click="userFollower(item,index)">
										<view :class="item.status==1?'author-follow':''">{{item.status==1?'已关注':'关注'}}
										</view>
									</view>
								</view>
							</block>
						</view>
					</scroll-view>
				</swiper-item>
			</swiper>
		</view>

	</view>
</template>

<script>
	import {
		getSearch,
		addFollow,
		removeFollow,
		getFollow
	} from "@/api/request"
	export default {
		data() {
			return {
				searchVal: "",
				navIndex: 0,
				isFollow: false,
				user_id: "",
				searchList: [],
				searchNavList: [{
						id: 0,
						title: "新闻"
					},
					{
						id: 1,
						title: "主题"
					},
					{
						id: 2,
						title: "媒体"
					}
				],
				topicList: [{
						id: 0,
						title: "时事",
						url: "../../static/image/newstype/ss.jpg",
						num: "19k"
					},
					{
						id: 1,
						title: "财经",
						url: "../../static/image/newstype/cj.jpg",
						num: "12k"
					},
					{
						id: 2,
						title: "思想",
						url: "../../static/image/newstype/sx.jpg",
						num: "3000"
					},
					{
						id: 3,
						title: "生活",
						url: "../../static/image/newstype/sh.jpg",
						num: "2000"
					},
					{
						id: 4,
						title: "历史",
						url: "../../static/image/newstype/ls.jpg",
						num: "10k"
					}
				],
				mediaList: [{
						id: 0,
						name: "澎湃新闻网",
						logo: "http://localhost:3000/images/pp.jpg",
						user_id: "ppxww-id-1",
						status: 0
					},
					{
						id: 1,
						name: "腾讯网",
						logo: "http://localhost:3000/images/tx.jpg",
						user_id: "txw-id-2",
						status: 0
					},
					{
						id: 2,
						name: "新浪网",
						logo: "http://localhost:3000/images/xl.jpg",
						user_id: "xlw-id-3",
						status: 0
					},
					{
						id: 3,
						name: "凤凰网",
						logo: "http://localhost:3000/images/fh.jpg",
						user_id: "ffw-id-4",
						status: 0
					},
					{
						id: 4,
						name: "搜狐网",
						logo: "http://localhost:3000/images/sh.jpg",
						user_id: "shw-id-5",
						status: 0
					},
					{
						id: 5,
						name: "光明网",
						logo: "http://localhost:3000/images/gm.jpg",
						user_id: "gmw-id-6",
						status: 0
					},
					{
						id: 6,
						name: "千龙网",
						logo: "http://localhost:3000/images/ql.jpg",
						user_id: "qlw-id-7",
						status: 0
					},
					{
						id: 7,
						name: "中国新闻网",
						logo: "http://localhost:3000/images/zgxww.jpg",
						user_id: "zgxww-id-8",
						status: 0
					}
				]
			}
		},
		methods: {
			//获取用户关注列表
			getUserFollowList() {
				let data = {
					user_id: this.user_id
				}
				getFollow(data).then(content => {
					// console.log(content);
					let {
						code,
						data
					} = content
					if (code == 200) {
						let m = this.mediaList;
						for (let i = 0; i < m.length; i++) {
							for (let j = 0; j < data.length; j++) {
								if (m[i].user_id == data[j].follow_user) {
									m[i].status = 1
								}
							}
						}
					}
				}).catch(err => {
					console.log(err);
				})
			},
			//点击关注
			userFollower(item, index) {
				if (this.mediaList[index].status == 0) {
					let user_id = localStorage.getItem("user_id")
					let data = {
						user_id,
						follow_user: item.user_id,
						follow_name: item.name,
						follow_avators: item.logo
					}
					console.log(data);
					addFollow(data).then(content => {
						// console.log(content);
						let {
							code,
							msg
						} = content
						if (code == 200) {
							uni.showToast({
								title: "关注成功",
								icon: "none"
							})
							this.mediaList[index].status = 1
						}
					}).catch(err => {
						console.log(err);
					})
				} else {
					let data = {
						user_id: this.user_id,
						follow_user: item.user_id,
					}
					removeFollow(data).then(content => {
						let {
							code,
							msg
						} = content
						if (code == 200) {
							uni.showToast({
								title: "取关成功",
								icon: "none"
							})
							this.mediaList[index].status = 0
						}
					}).catch(err => {
						console.log(err);
					})
				}

			},
			//去主题列表
			goTopic(title) {
				uni.navigateTo({
					url: "/pages/topic/topic?type=" + title
				})
			},
			//去详情页
			goDetail(id) {
				console.log(id);
				uni.navigateTo({
					url: "/pages/detail/detail?id=" + id
				})
			},
			swipeIndex(e) {
				this.navIndex = e.detail.current
			},
			handoff(index) {
				this.navIndex = index
			},
			getSearchNewsList() {
				console.log(this.searchVal);
				let data = {
					searchVal: this.searchVal
				}
				uni.showLoading({
					title: "加载中..."
				})
				getSearch(data).then(content => {
					// console.log(content);
					let {
						code,
						data
					} = content
					if (code == 200) {
						this.searchList = data
						uni.hideLoading();
					}

				}).catch(err => {
					console.log(err);
				})
			}
		},
		onLoad(e) {
			if (e.index) {
				this.navIndex = e.index
			}
		},
		created() {
			this.user_id = localStorage.getItem("user_id");
			this.getUserFollowList();
		}

	}
</script>

<style lang="scss" scoped>
	.search {
		width: 100%;
		height: 100vh;
		padding-top: $uni-box-padding;
	}

	.search-box {
		position: relative;
		width: 100%;
		padding: 0 30rpx;

		.search {
			height: 96rpx;
			padding: 20rpx 64rpx 20rpx 74rpx;
			// box-sizing: border-box;s
			border: 1px solid #A0A3BD;
			border-radius: 14rpx;
			font-size: 24rpx;
		}

		.search-left-icon,
		.search-right-icon {
			display: block;
			width: 48rpx;
			height: 48rpx;
			// background-color: #000;
			transform: translateY(-50%);
			position: absolute;
			top: 50%;
			background-repeat: no-repeat;
			background-size: 100%;
		}

		.search-left-icon {
			background-image: url("@/static/image/search.png");
			left: 40rpx;
		}

		.search-right-icon {
			// background-image: url("@/static/image/set.png");
			text-align: center;
			line-height: 48rpx;
			width: 70rpx;
			font-size: 28rpx;
			right: 40rpx;

		}
	}

	.nav-ul-list {
		padding: 30rpx 0;
		width: 100%;
		display: flex;
		align-items: center;
		justify-content: center;

		.nav-li-content {
			padding: 0 20rpx;

			.nav-title {
				font-size: 32rpx;
				color: #4E4B66;
				padding-bottom: 10rpx;
			}

			.underline {
				margin: 0 auto;
				display: block;
				width: 20rpx;
				height: 4rpx;
				background-color: transparent;
				transition: width 0.3s;

				&.active {
					width: 60rpx;
					background-color: #1877F2;
				}
			}
		}
	}

	.search-nav-list {
		// border: 1px solid #000;
		width: 100%;
		// height: 100%;
		height: calc(100% - 116rpx - 96rpx);
		// height:100vh;

		.scroll-view-height {
			height: 100%;
		}

		.scroll-height {
			height: 100%;

		}

		::-webkit-scrollbar {
			width: 0;
			height: 0;
			background-color: transparent;
		}

		swiper-item {
			padding: 0 30rpx;
		}

		.section-list {
			// border: 1px solid #000;
			// margin-top: 30rpx;
			width: 100%;
			height: 500rpx;

			.search-null {
				// border: 1px solid #000;
				width: 100%;

				.search-null-header {
					display: flex;
					align-items: center;
				}

				.null-left {
					border-radius: 10rpx;
					width: 6rpx;
					height: 25rpx;
					background-color: #1877F2;
				}

				.null-right {
					font-size: 28rpx;
					padding-left: 10rpx;
					color: #000;
					font-weight: bold;
				}

				.want-search-text {
					padding: 15rpx 0;
				}
			}

			.li-list {
				display: flex;
				align-items: center;
				margin-bottom: 30rpx;

				.left-img {
					width: 192rpx;
					height: 192rpx;

					image {
						border-radius: 10rpx;
						width: 100%;
						height: 100%;
					}
				}

				.content-writing {
					// width: 100%;
					flex: 1;
					padding: 0 10rpx;

					.p-location {
						font-size: 28rpx;
						color: $uni-text-color-grey;
					}

					.p-title {
						padding: 15rpx 0 10rpx 0;
						font-size: 32rpx;
						color: #000;
						height: 106rpx;
						overflow: hidden;
						// text-overflow: ellipsis;
						-webkit-line-clamp: 2;
						-webkit-box-orient: vertical;
						display: -webkit-box;
					}

					.p-come {
						width: 100%;
						display: flex;
						align-items: center;
						justify-content: space-between;
					}

					.p-come-left {
						display: flex;
						align-items: flex-end;
					}

					.p-person {
						display: flex;
						align-items: center;
						margin-right: 20rpx;
					}


					.person-icon {
						display: block;
						width: 40rpx;
						height: 40rpx;
						// background-color: #000;
						border-radius: 50%;
						margin-right: 5rpx;
						image{
							border: 1px solid #f0f0f0;
							border-radius: 50%;
							width: 100%;
							height: 100%;
						}
					}

					.person {
						padding-left: 5rpx;
						font-size: 28rpx;
						color: #4e4b46;
						font-weight: bold;
					}

					.p-time {
						padding-left: 10rpx;
						font-size: 20rpx;
						color: $uni-text-color-grey;
					}

					.p-come-right {
						width: 40rpx;
						height: 40rpx;
						// background-color: #000;
						background-image: url("@/static/image/more.png");
						background-repeat: no-repeat;
						background-size: 100%;
					}
				}
			}
		}

		.ul-topic {
			width: 100%;
			// padding: 10rpx 0;

			.li-topic {
				position: relative;
				// padding: 10rpx;
				// margin-bottom: 30rpx;
				width: 100%;
				display: flex;
				align-items: center;
				// justify-content: space-between;

				.topic-img {
					position: absolute;
					top: 0;
					left: 0;
					width: 100%;
					// width: 140rpx;
					height: 160rpx;
					z-index: -1;

					image {
						width: 100%;
						height: 100%;
					}
				}

				.topic-desc {
					padding: 0 60rpx;
					width: 100%;
					// padding-left: 10rpx;
					display: flex;
					align-items: center;
					// flex-flow: column; //垂直排列
					justify-content: space-between; //两端对齐
					height: 160rpx;
					background-color: rgba(0, 0, 0, .4);

					.desc-title {
						position: absolute;
						top: 50%;
						left: 35px;
						// padding-bottom: 10rpx;
						font-size: 44rpx;
						font-weight: bold;
						color: #fff;
					}

					.desc-exp {
						position: absolute;
						top: 43%;
						right: 15px;
						letter-spacing: 2rpx;
						line-height: 40rpx;
						font-size: 24rpx;
						color: #f5f5f5;
					}
				}

				.topic-butt {
					flex-grow: 1;

					// text-align: right;
					.butt-style {
						width: 126rpx;
						height: 64rpx;
						line-height: 64rpx;
						// margin: 0 auto;
						margin-left: auto;
						text-align: center;
						font-size: 32rpx;
						// color: $uni-color-primary;
						color: #fff;
						// border: 1px solid $uni-color-primary;
						background-color: $uni-color-primary;
						border-radius: 10rpx;
					}
				}
			}
		}
	}

	.ul-author {
		width: 100%;

		.li-author {
			padding-bottom: 40rpx;
			display: flex;
			align-items: center;
			justify-content: space-between;

		}

		.author-left {
			display: flex;
			align-items: center;

			.author-img {
				width: 100rpx;
				height: 100rpx;

				image {
					border: 1px solid #f0f0f0;
					border-radius: 50%;
					width: 100%;
					height: 100%;
				}
			}

			.author-name {
				padding-left: 20rpx;
				font-size: 36rpx;
				color: #000;
				font-weight: bold;
			}
		}

		.author-right {
			background-color: #1877F2;
			border-radius: 10rpx;
			font-size: 32rpx;
			color: #fff;
			width: 136rpx;
			height: 66rpx;
			line-height: 66rpx;
			text-align: center;

			.author-follow {
				text-align: center;
				background-color: #fff;
				border: 1px solid #1877F2;
				font-size: 32rpx;
				width: 136rpx;
				color: #1877F2;
				border-radius: 10rpx;
			}
		}
	}
</style>